//
//  ZCEmoticonKeyBoardCell.swift
//  weibo
//
//  Created by Ning Xie on 16/8/18.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit

let DidSelectedEmoticonButtonActionNotification = "DidSelectedEmoticonButtonActionNotification"
let EmoticonDeleteButtonActionNotification = "EmoticonDeleteButtonActionNotification"
let EmoticonDeleteButtonLongPressNotification = "EmoticonDeleteButtonLongPressNotification"


class ZCEmoticonKeyBoardCell: UICollectionViewCell {
    
    var emoticonModel:[ZCEmoticonModel]?{
        didSet{
            
            guard let model = emoticonModel else{
                return
            }
            for value in emoticonButtonArray{
                value.hidden = true
            }
            
            for (i, value) in model.enumerate() {
                
                let emoticonBtn = emoticonButtonArray[i]
                emoticonBtn.hidden = false
                emoticonBtn.emoticonModel = value
                
                if value.type == "0" {
                    emoticonBtn.setImage(UIImage(named: value.path!), forState: .Normal)
                    emoticonBtn.setTitle(nil, forState: .Normal)
                }else{
                    emoticonBtn.setImage(nil, forState: .Normal)
                    emoticonBtn.setTitle((value.code! as NSString).emoji(), forState: .Normal)
                }
            }
        }
    }
    
    
    //用于储存每个cell上的20个button的数组
    private var emoticonButtonArray: [ZCEmoticonButton] = [ZCEmoticonButton]()
    private var emoticonDeleteButton:UIButton = UIButton()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI(){
        
        addChildButton()
        addDeleteBuuton()
        
        backgroundColor = UIColor(patternImage: UIImage(named: "emoticon_keyboard_background")!)
        
    }
    
    //添加每页的表情
    private func addChildButton(){
        
        for _ in 0..<20 {
            
            let button = ZCEmoticonButton()
            button.titleLabel?.font = UIFont.systemFontOfSize(30)
            button.addTarget(self, action: #selector(ZCEmoticonKeyBoardCell.didSelectedEmoticonButtonAction(_:)), forControlEvents: .TouchUpInside)
            
            
            contentView.addSubview(button)
            emoticonButtonArray.append(button)
        }
    }
    
    //添加每页当中的删除按钮
    private func addDeleteBuuton(){
        
        emoticonDeleteButton.setImage(UIImage(named: "compose_emotion_delete"), forState: .Normal)
        emoticonDeleteButton.setImage(UIImage(named: "compose_emotion_delete_highlighted"), forState: .Highlighted)
        emoticonDeleteButton.addTarget(self, action: #selector(ZCEmoticonKeyBoardCell.deleteButtonAction), forControlEvents: .TouchUpInside)
        let longPressGR = UILongPressGestureRecognizer(target: self, action: #selector(ZCEmoticonKeyBoardCell.deleteButtonLongPress))
        emoticonDeleteButton.addGestureRecognizer(longPressGR)
        contentView.addSubview(emoticonDeleteButton)
    }
    
    //点击每个表情按钮的方法
    @objc private func didSelectedEmoticonButtonAction(button:ZCEmoticonButton){
        
        NSNotificationCenter.defaultCenter().postNotificationName(DidSelectedEmoticonButtonActionNotification, object: button)
    }
    //点击删除按钮的点击方法
    @objc private func deleteButtonAction(){
        NSNotificationCenter.defaultCenter().postNotificationName(EmoticonDeleteButtonActionNotification, object: nil)
    }
    //长按删除按钮的方法
    @objc private func deleteButtonLongPress(){
        
        NSNotificationCenter.defaultCenter().postNotificationName(EmoticonDeleteButtonLongPressNotification, object: nil)
    }
    
    
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        let itemWidth = self.bounds.size.width / 7
        let itemHeight = self.bounds.size.height / 3
        
        for (i,value) in emoticonButtonArray.enumerate(){
            
            let col = i % 7
            let row = i / 7
            
            let itemX = CGFloat(col) * itemWidth
            let itemY = CGFloat(row) * itemHeight
            
            value.frame = CGRectMake(itemX, itemY, itemWidth, itemHeight)
        }
        let deleteBtnX = contentView.width - itemWidth
        let deleteBtnY = contentView.height - itemHeight
        emoticonDeleteButton.frame = CGRectMake(deleteBtnX, deleteBtnY, itemWidth, itemHeight)
    }
}
